home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: newsfeed.internetmci.com!miwok!linex1!news
- From: mfried@linex.com (Marty Fried)
- Subject: Re: HELP: A basic question
- X-Nntp-Posting-Host: sp106.linex.com
- Message-ID: <316ff292.4129426@news.linex.com>
- Sender: news@linex1.linex.com
- Organization: Cirrius Cybernetics Corp
- X-Newsreader: Forte Agent .99e/32.209
- References: <4kfl8c$nju@falcon.ccs.uwo.ca>
- Date: Sat, 13 Apr 1996 18:38:49 GMT
-
- Once upon a time (OK, it was 10 Apr 1996 06:42:20 GMT), Sharon Wang
- <swang1@julian.uwo.ca> wrote:
-
- >hello worlf, i have the following code which compiles ok but causes
- >segmentation fault:
- >
- >class T{
- > ...
- >public:
- > ...
- > T deform();
- >};
- >
- >T T::deform(void)
- >{
- > T c;
- > ...
- > return c; // guess something wrong here
- >}
- >
- >int main(void)
- >{
- > T a, b;
- > ...
- > b = a.deform(); // ERROR: segmentation fault! ('=' overloaded ok)
- >}
- >
- >can anyone tell me what's wrong with the code, or please tell me
- >if this is not the right place to post.
-
- One problem I see is the body of deform - you are declaring a c as a
- stack variable of type T, meaning it is destroyed at the end of the
- function, yet you are returning it after it is destroyed. You could
- use new, and return a pointer.
- ie
- T *T::deform()
- {
- T *c = new T;
- return c;
- }
-
- int main()
- {
- T a, *b;
- b = a.deform();
- }
- Be sure to delete b when done.
-
- There may be another way, but this was the simplest I thought of right
- off the bat.
-
- _______________________________________________________
- Marty Fried - mfried@linex.com Press Enter to Exit
- San Anselmo, CA -NT message
- (MSVC4 + MFC) && (Win95 || NT);
-